﻿using EkStudio.com.bean.xml.reflection;
using EkStudio.com.bean.xml.RootInterface;
using System;
using System.Collections.Generic;
using System.Linq;

namespace EkStudio.com.bean.xml.WorkFlow.Node
{
    /// <summary>
    /// 节点根目录
    /// </summary>
    public class WflowModuleNodeRoot : BillUiFormatContainer
    {
        private int maxid = 0;

        private List<WflowModuleNodeItem> flowCommandNode = null;

        [Xml]
        public int Maxid
        {
            get
            {
                return maxid;
            }

            set
            {
                maxid = value;
            }
        }

        [XmlTransient]
        public object ParentBean
        {
            get
            {
                return null;
            }

            set
            {
                
            }
        }
        [XmlElement]
        public List<WflowModuleNodeItem> FlowCommandNode
        {
            get
            {
                return flowCommandNode;
            }

            set
            {
                flowCommandNode = value;
            }
        }

        public string getNLanguageText(BillFormatModule format)
        {
            String result = "";
            int tempMaxid = maxid + 1;
            if (format != null)
            {
                string text = format.getLanguageText();
                int index = text.IndexOf("Caption");
                int length = text.Length - 7;
                result = text.Substring(0, length);
                result = result + "Node[" + tempMaxid + "].Caption";
            }
            this.Maxid = tempMaxid;
            return result;
        }
        /// <summary>
        /// 修改所有节点的多语言
        /// </summary>
        /// <param name="format">单据名称</param>
        public void updateLanguageText(BillFormatModule format)
        {
            this.maxid = 0;
            if (this.flowCommandNode != null && this.flowCommandNode.Count > 0)
            {
                foreach (WflowModuleNodeItem group in this.flowCommandNode)
                {
                    group.LanguageTexts = this.getNLanguageText(format);
                }
            }
        }



        public object Clone()
        {
            WflowModuleNodeRoot clone = new WflowModuleNodeRoot();
            clone.Maxid = maxid;
            clone.FlowCommandNode = new List<WflowModuleNodeItem>();
            if (flowCommandNode != null && flowCommandNode.Count > 0)
            {
                foreach(WflowModuleNodeItem it in flowCommandNode)
                {
                    WflowModuleNodeItem cl = it.Clone() as WflowModuleNodeItem;
                    if(cl!=null)
                        clone.FlowCommandNode.Add(it.Clone() as WflowModuleNodeItem);
                }
            }
            return clone;
        }

        public void addNode(BillUiFormatNode node, BillFormatModule format)
        {
            WflowModuleNodeItem it = node as WflowModuleNodeItem;
            if (it != null)
            {
                it.LanguageTexts = getNLanguageText(format);
                if (flowCommandNode == null) flowCommandNode = new List<WflowModuleNodeItem>();
                flowCommandNode.Add(it);
            }
        }

        public void removeNode(BillUiFormatNode node)
        {
            WflowModuleNodeItem it = node as WflowModuleNodeItem;
            if (it != null&&flowCommandNode.Contains(it))
            {
                flowCommandNode.Remove(it);
            }
        }

        public void moveUp(BillUiFormatNode node)
        {
            
        }

        public void moveDown(BillUiFormatNode node)
        {
            
        }
    }
}
